package sky.controller;

import sky.JULLog.MyLogger;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author : wushikai
 * <p>
 * date : 2022-05-10
 */
@WebServlet(name = "HelloController" , urlPatterns = ( "/hello"))
public class HelloController extends HttpServlet {


    private  static  Logger log  = MyLogger.getLogger(HelloController.class) ;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        log.info("我是被反射调用过来的.....");
        log.info(config.getServletContext().getContextPath());


    }

    /**
     * http://localhost:8080/sky/hello
     *
     * */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.write("hello,  this is helloController!!");
        writer.flush();
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = req.getServletContext();
        PrintWriter writer = resp.getWriter();
        writer.write("hello,  this is helloController!!");
        writer.flush();
    }



    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }



    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        super.service(req, res);

        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        for (StackTraceElement[] value : allStackTraces.values()) {
            for (StackTraceElement element : value) {
               log.info( element.getClassName() + element.getMethodName() + element.getLineNumber());
            }
        }


        log.info("我什么时候被调用的? ");

    }
}
